function writeFile(key, value) {
  localStorage.setItem(key, value);
}
class Storage {
  constructor(fileId) {
    this._fileId = fileId || "global";
    this._data = null;
  }

  checkStorage() {
    if (!this._data) {
      this._data = JSON.parse(localStorage.getItem(String(this._fileId)));
    }
  }

  set(key, value) {
    this.checkStorage();
    this._data[key] = value;
    writeFile(this._fileId, JSON.stringify(this._data));
  }
  async get(key) {
    this.checkStorage();
    return this._data[key];
  }
}

async function createStorageTo(fileId) {
  if (!localStorage.getItem(fileId)) {
    localStorage.setItem(fileId, JSON.stringify({}));
  }
  return new Storage(fileId);
}

module.exports = {
  createStorageTo,
};
